# -*- coding: UTF-8 -*-
# __author__ = 'Sengo'
from settings import AMBIENT_COLOR, Kd, Ks, I, REFLECT_N
from base_service import vertex_dot_multiply
import math
import numpy as np


def computer_I(N, V, L, Od):
    # R
    H = (np.mat(L) + np.mat(V)) / np.linalg.norm(np.mat(L) + np.mat(V))
    H = H.tolist()[0]
    color = []
    for i in range(3):
        Ir = AMBIENT_COLOR[i] + \
             I[i] * (Kd * Od[i]*1.0/255 * vertex_dot_multiply(N, L) +
                                   Ks * Od[i]*1.0/255 * math.pow(vertex_dot_multiply(N, H), REFLECT_N))
        color.append(Ir)
    return color


def computer_gouraud_normal(lines):
    vertex_normals = {}
    for line in lines:
        N = line[-3:-2][0]
        for v in line[:-3]:
            if not vertex_normals.get(v):
                vertex_normals[v] = [N]
            else:
                vertex_normals[v].append(N)

    vertex_gouraud_normal = {}
    for key, value in vertex_normals.items():
        up = np.mat([0.0, 0.0, 0.0])
        for n in value:
            up += np.mat(n)
        gouraud_normal = up / np.linalg.norm(up)
        vertex_gouraud_normal[key] = gouraud_normal.tolist()[0]
    return vertex_gouraud_normal


if __name__ == '__main__':
    print math.pow(2, 3)
